cc.Class({
    extends: cc.Component,

    properties: {
        m: 1,
        n: 1,
        },
    // use this for initialization
    //生成随机数，决定按钮的颜色
    sjs: function() {
        var button = this.getComponent(cc.Button);
        this.m = Math.floor(Math.random()*2);
        if(this.m==0){
            button.normalColor = new cc.Color(0, 0, 255);
            button.hoverColor = new cc.Color(0, 0, 255);
            button.pressedColor = new cc.Color(0, 0, 255);
            this.n = 0;
         }else{
            button.normalColor = new cc.Color(255,255,255);
            button.hoverColor = new cc.Color(255,255,255);
            button.pressedColor = new cc.Color(255,255,255);
            this.n = 1;
        }
    },
    start: function() {
        var _this = this;
        //做一个循环，使颜色每3秒改变一次
        var it = setInterval(function () {
        _this.sjs();
      }, 3000);
        var button = this.getComponent(cc.Button);
        //点击鼠标，根据n的值判断是否有地鼠，然后再改变button的颜色，同时发射消息
        this.node.on("click",function(event){
            if(_this.n===0){//有地鼠，颜色重新变回白色
                button.normalColor = new cc.Color(255,255,255);
                button.hoverColor = new cc.Color(255,255,255);
                button.pressedColor = new cc.Color(255,255,255);
                _this.n = 1;
                _this.node.dispatchEvent( new cc.Event.EventCustom('233', true) );
                console.log("good");//调试时使用，用来判断是否运行了此语句
            }else if(_this.n===1){//没有地鼠，颜色变成红色
                button.normalColor = new cc.Color(255,0,0);
                button.hoverColor = new cc.Color(255,0,0);
                button.pressedColor = new cc.Color(255,0,0);
                console.log("bad");//调试时使用，用来判断是否运行了此语句
                _this.n = 2;
                _this.node.dispatchEvent( new cc.Event.EventCustom('244', true) );
            }else if(_this.n===2){//为红色时，不可点击
                button.normalColor = new cc.Color(255,0,0);
                button.hoverColor = new cc.Color(255,0,0);
                button.pressedColor = new cc.Color(255,0,0);
            }
        });
    },
    
    onLoad: function () {

    },
});
